import qs from "qs";
// json格式请求头 仅post方式设置，并且需要开启
const headerJSON = {
  "Content-Type": "application/json",
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Credentials": true,
  "Access-Control-Allow-Headers": "Content-Type,Content-Length,Authorization, Accept,X-Requested-With",
  "Access-Control-Allow-Methods": "PUT,POST,GET,DELETE,HEAD,OPTIONS"
};
// FormData格式请求头
const headerFormData = {
  "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Credentials": true,
  "Access-Control-Allow-Headers": "Content-Type,Content-Length,Authorization, Accept,X-Requested-With",
  "Access-Control-Allow-Methods": "PUT,POST,GET,DELETE,HEAD,OPTIONS"
};

const http = {
	 BASE_URL: "http://www.shangchuangnet.com:8080",
     // BASE_URL: "http://localhost:8080",
  /**
   * methods: 请求
   * @param url 请求地址
   * @param params 请求参数
   * @param json 判断数据发送是否是json格式，仅post方式用到
   */
  get (url, params) {
    const config = {
      method: "get",
      url: this.BASE_URL+url,
      header: headerFormData,
	  dataType: "json"
    };
	console.log(config)
    if (params) config.data = params;
    return uni.request(config);
  },
  post (url, params, json) {
    const config = {
      method: "post",
      url: this.BASE_URL+url,
      header: json==='json' ? headerJSON : headerFormData,
	  dataType: "json"
    };
	console.log("config",config)
    if (params) config.data = json==='json' ? params : qs.stringify(params);
    return uni.request(config);
  },
  download (url, params, json) {
    const config = {
      method: "get",
      url: this.BASE_URL+url,
      header: json ? headerJSON : headerFormData,
      responseType: 'blob',
    };
    if (params) config.data = json ? params : qs.stringify(params);

    return uni.request(config);
  },
  postFile (url, params) {
    const config = {
      method: "post",
      url: this.BASE_URL+url,
      header: { 'Content-type': 'multipart/form-data' }
    };
    if (params) config.data = params;

    return uni.request(config);
  },
  put (url, params) {
    const config = {
      method: "put",
      url: this.BASE_URL+url,
      header: headerFormData
    };
    if (params) config.data = params;
    return uni.request(config);
  },
  delete (url, params) {
    const config = {
      method: "delete",
      url: this.BASE_URL+url,
      header: headerFormData
    };
    if (params) config.data = params;
    return uni.request(config);
  },
};
//导出
export default http;
